﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.Farmings_NextWay;
using Traceability_Management.Read.Api.Application.Command.SlaughtsWay_NextWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.SlaughtsWay_NextWay
{
    public class FirstNextSlaughtsCommandHandler : IRequestHandler<FirstNextSlaughtsCommand, ApiResult<Slaughts_Next>>
    {
        IBaseReponsitory<Slaughts_Next> Slaughts_Next;

        public FirstNextSlaughtsCommandHandler(IBaseReponsitory<Slaughts_Next> Slaughts_Next)
        {
            this.Slaughts_Next = Slaughts_Next;
        }

        public async Task<ApiResult<Slaughts_Next>> Handle(FirstNextSlaughtsCommand request, CancellationToken cancellationToken)
        {
            var Bachlist = await Slaughts_Next.GetQuery().FirstOrDefaultAsync(c => c.Rabit_Batch == request.Batch);
            if (Bachlist != null)
            {
                return new ApiResult<Slaughts_Next>
                {
                    Code = ApiEnum.Success,
                    Message = "加载成功了",
                    Data = Bachlist
                };
            }
            else
            {
                return new ApiResult<Slaughts_Next>
                {
                    Code = ApiEnum.Success,
                    Message = "请设置信息",
                };
            }
        }
    }
}
